package GUI;

import Data.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import sun.audio.*;

public class ADVFrame extends JFrame
{
	private Container container;
	private ChoosePanel cp;
	private MainPanel mp;
	private ScriptPanel sp;
	private JPanel blank1,blank2;
	private JMenuItem autoItem,skipItem;
	
	private boolean auto = false,select = false,skip = false;
	private int delay,font1,font2,temp,type;
	private String text,controls[],selects[];
	private File file;
	private Timer timer,wait;
	private ADVAnalysis analysis;
	private InputStream is1,is2;
	private AudioStream as1,as2;
	
	public ADVFrame(File f)
	{
		super(f.getName().substring(0,f.getName().length() - 7));
		file = f;
		analysis = new ADVAnalysis(file);
		
		container = getContentPane();
		container.setLayout(new BorderLayout(0,0));
		
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		
		JMenu gameMenu = new JMenu("Game");
		gameMenu.setMnemonic('G');
		
		JMenuItem exitItem = new JMenuItem("Exit");
		exitItem.setMnemonic('x');
		
		exitItem.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					System.exit(0);
				}
			}
		);
		
		gameMenu.add(exitItem);
		
		bar.add(gameMenu);
		
		JMenu messageMenu = new JMenu("Message");
		messageMenu.setMnemonic('M');
		
		skipItem = new JMenuItem("Skip");
		skipItem.setMnemonic('S');
		
		skipItem.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					autoItem.setEnabled(false);
					skipItem.setEnabled(false);
					auto = false;
					skip = true;
					proceed(-1);
				}
			}
		);
		
		messageMenu.add(skipItem);
		
		autoItem = new JMenuItem("Auto");
		autoItem.setMnemonic('A');
		
		autoItem.addActionListener
		(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent event)
				{
					autoItem.setEnabled(false);
					skipItem.setEnabled(false);
					auto = true;
					skip = false;
					proceed(-1);
				}
			}
		);
		
		messageMenu.add(autoItem);
		
		bar.add(messageMenu);
		
		mp = new MainPanel();
		container.add(mp);
		
		mp.addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent event)
				{
					if (!select)
					{
						execute();
					}
				}
			}
		);
		
		blank1 = new JPanel();
		blank1.setOpaque(false);
		blank1.setPreferredSize(new Dimension(800,90));
		mp.add(blank1);
		
		cp = new ChoosePanel();
		mp.add(cp);
		
		cp.addMouseListener
		(
			new MouseAdapter()
			{
				public void mouseClicked(MouseEvent e)
				{
					if (cp.getChoose() > -1 && cp.getChoose() < 4)
					{
						temp = cp.getChoose();
						select = false;
						autoItem.setEnabled(true);
						skipItem.setEnabled(true);
						cp.clear();
						proceed(temp);
					}
				}
			}
		);
		
		blank2 = new JPanel();
		blank2.setOpaque(false);
		blank2.setPreferredSize(new Dimension(800,90));
		mp.add(blank2);
		
		sp = new ScriptPanel();
		mp.add(sp);
		
		timer = new Timer(0,null);
		wait = new Timer(0,null);
		proceed(-1);
		
		setResizable(false);
		setSize(800,600);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public void execute()
	{
		if (!select)
		{
			if (auto || skip)
			{
				if (auto)
				{
					auto = false;
					
				}
				else
				{
					skip = false;
				}
				
				if (timer.isRunning())
				{
					timer.stop();
					timer.setDelay(50);
					timer.start();
				}
				
				autoItem.setEnabled(true);
				skipItem.setEnabled(true);
			}
			else
			{
				proceed(-1);
			}
		}
	}
	
	public void proceed(int i)
	{
		if (!timer.isRunning() && !wait.isRunning())
		{
			controls = analysis.getDialogue(i);
			type = Integer.parseInt(controls[0]);
			
			switch(type)
			{
			case 0:
				sp.setVisible(true);
				text = "";
			case 1:
				temp = 0;
				delay = skip?1:50;
				
				timer = new Timer(delay,new ActionListener()
				{
					public void actionPerformed(ActionEvent event)
					{
						if (temp == controls[1].length())
						{
							timer.stop();
							
							if (auto || skip)
							{
								proceed(-1);
							}
						}
						else
						{
							text += controls[1].substring(temp,temp + 1);
							sp.setText(text);
							temp++;
						}
					}
				});
				
				timer.start();
				break;
			case 2:
				sp.setVisible(true);
				temp = 0;
				font1 = Integer.parseInt(controls[1]);
				font2 = Integer.parseInt(controls[2]);
				text = controls[3];
				sp.setColor(font2);
				sp.setText(text);
				wait(200);
				break;
			case 3:
				delay = skip?50:Integer.parseInt(controls[1]) * 50 / 3;
				wait(delay);
				break;
			case 4:
				mp.clear();
				mp.setBackground(controls[1]);
				wait(200);
				break;
			case 5:
				if (controls[1].equals("0"))
				{
					mp.setBackground(Color.BLACK);
				}
				else
				{
					mp.setBackground(Color.WHITE);
				}
				
				mp.clear();
				sp.setVisible(false);
				wait(200);
				break;
			case 6:
				wait(1);
				break;
			case 7:
				mp.setForeground(true,Integer.parseInt(controls[3]),controls[1]);
				mp.setForeground(false,3 - Integer.parseInt(controls[3]),controls[2]);
				wait(200);
				break;
			case 8:
				mp.setForeground(true,mp.getCharacter(true),"");
				mp.setForeground(false,mp.getCharacter(false),"");
				wait(200);
				break;
			case 9:
				mp.setForeground(controls[1].equals("0"),Integer.parseInt(controls[3]),controls[2]);
				wait(200);
				break;
			case 10:
				mp.setForeground(controls[1].equals("0"),mp.getCharacter(controls[1].equals("0")),"");
				wait(200);
				break;
			case 11:
				controls[3] = mp.getForeground(controls[1].equals("0"));
				mp.setForeground(controls[1].equals("0"),mp.getCharacter(controls[1].equals("0")),"");
				mp.setForeground(controls[1].equals("0"),Integer.parseInt(controls[2]),controls[3]);
				wait(200);
				break;
			case 12:
				if (controls[1].equals("2"))
				{
					sp.setVisible(false);
				}
				
				wait(200);
				break;
			case 13:
				wait(1);
				break;
			case 14:
				wait(1);
				break;
			case 15:
				try
				{
					is1 = new FileInputStream("data\\bgm\\track" + controls[1] + ".wav");
					as1 = new AudioStream(is1);
					AudioPlayer.player.start(as1);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				wait(200);
				break;
			case 16:
				AudioPlayer.player.stop(as1);
				wait(200);
				break;
			case 17:
				try
				{
					is2 = new FileInputStream("data\\se\\SE" + controls[1] + ".wav");
					as2 = new AudioStream(is1);
					AudioPlayer.player.start(as2);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				
				wait(200);
				break;
			case 18:
				AudioPlayer.player.stop(as2);
				wait(200);
				break;
			case 19:
				wait(1);
				break;
			case 20:
				wait(1);
				break;
			case 100:
				auto = false;
				skip = false;
				select = true;
				autoItem.setEnabled(false);
				skipItem.setEnabled(false);
				selects = controls[1].split("\n");
				cp.setText(selects);
				break;
			case 666:
				System.exit(0);
			default:
			}
		}
		else
		{
			if (timer.isRunning())
			{
				timer.stop();
				text += controls[1].substring(temp,controls[1].length());
				sp.setText(text);
				
				if (auto || skip)
				{
					proceed(-1);
				}
			}
		}
	}
	
	public void wait(int i)
	{
		if (skip)
		{
			i = 1;
		}
		
		wait = new Timer(i,new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				wait.stop();
				proceed(-1);
			}
		});
		
		wait.start();
	}
}